package com.dy.video.controller;

import com.dy.video.Video;
import com.dy.video.mapper.VideoMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaoshi.common.config.Result;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("video1")
public class ClientController {

    @Autowired
    private VideoMapper videoMapper;

    // 创建Elasticsearch客户端
    private RestHighLevelClient createClient() {
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);
        return new RestHighLevelClient(builder);
    }

    // 查询ES索引中所有数据
    @RequestMapping("findList")
    public Result findAll() {
        try (RestHighLevelClient client = createClient()) {
            // 编写查询操作
            SearchRequest searchRequest = new SearchRequest("video");
            SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
            searchRequest.source(query);

            //发送请求
            SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = response.getHits();

            // 提取查询结果
            List<Map<String, Object>> results = new ArrayList<>();
            for (SearchHit hit : hits) {
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                results.add(sourceAsMap);
            }

            return Result.ok(results);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("Error searching Elasticsearch");
        }
    }

    @RequestMapping("findAdd")
    public Result<List<Video>> findAdd(Video video) throws IOException {
        // 从数据库获取数据
        List<Video> videoList = videoMapper.findList(video);
        System.out.println("1111111111111111111111111111"+videoList);
        //创建java和es的链接
        HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(httpHost);
        RestHighLevelClient client = new RestHighLevelClient(builder);
        System.out.println(client.indices());
        IndexRequest indexRequest = new IndexRequest();
        // 创建IndexRequest并指定类型
        //IndexRequest indexRequest = new IndexRequest("your_index_name", "your_type_name");
        indexRequest.index("video");
        // 将Java对象转换成JSON格式
        ObjectMapper mapper = new ObjectMapper();
        for (Video video1 : videoList) {
            indexRequest.id(video1.getId().toString());
            String jsongood = mapper.writeValueAsString(video1);
            // 设置源数据和内容类型
            indexRequest.source(jsongood, XContentType.JSON);
            // 发送请求，添加数据
            IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
            System.out.println("添加数据的结果：" + response.getIndex());
        }


        //关闭连接
        client.close();
        return null;
    }

}
